package dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import entity.Admin;
import util.db.DBhelp;
import util.db.RowMapper;

public class AdminDao {
	DBhelp<Admin> dBhelp = new DBhelp<Admin>();
	
	
	public Admin findAdminByUsername(String username) {
		System.out.println(username);
		String sql = "SELECT id,username,`password`,email,ip,role,lastaccesstime,createtime,orderaccesstime,commentaccesstime FROM t_admin WHERE username=?";
		
		return dBhelp.executeQueryToObject(sql, new adminRowMapper(), username);
	}
	

	public void update(Admin admin) {
		String sql = "update t_admin set username=?,password=?,email=?,ip=?,role=?,lastaccesstime=?,orderaccesstime=?,commentaccesstime=? where id=?";
		dBhelp.update(sql, admin.getUsername(),admin.getPassword(),admin.getEmail(),admin.getIp(),admin.getRole(),admin.getLastaccesstime(),admin.getOrderaccesstime(),admin.getCommentaccesstime(),admin.getId());
		
	}

	
	
	private class adminRowMapper implements RowMapper<Admin>{

		public Admin mapRow(ResultSet resultSet) throws SQLException {
			Admin admin = new Admin();
			admin.setId(resultSet.getString("id"));
			admin.setUsername(resultSet.getString("username"));
			admin.setPassword(resultSet.getString("password"));
			admin.setEmail(resultSet.getString("email"));
			admin.setIp(resultSet.getString("ip"));
			admin.setRole(resultSet.getString("role"));
			admin.setLastaccesstime(resultSet.getString("lastaccesstime"));
			admin.setCreatetime(resultSet.getString("createtime"));
			admin.setOrderaccesstime(resultSet.getString("orderaccesstime"));
			admin.setCommentaccesstime(resultSet.getString("commentaccesstime"));
			return admin;
		}
		
	}






	
}
